package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.DrugMapper;
/*import com.baiku.sanhos.mapper.search.DrugSearchRepository;*/
import com.baiku.sanhos.pojo.dto.DeptDTO;
import com.baiku.sanhos.pojo.dto.DrugDTO;
import com.baiku.sanhos.pojo.entity.Drug;
import com.baiku.sanhos.pojo.vo.DrugVo;
import com.baiku.sanhos.response.RequestDataLimit;
import com.baiku.sanhos.response.ResultVO;
import com.baiku.sanhos.response.StatusCode;
import com.baiku.sanhos.utils.RedisKeys;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "药品管理")
@RestController
@RequestMapping("/pa/drug")
public class DrugController {
  /* @Autowired
    DrugSearchRepository searchRepository;*/
    @Autowired
    DrugMapper mapper;
    @Autowired
    Drug drug;
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;

    @ApiOperation("查看所有药品信息")
    @GetMapping("")//不要加权限
    public ResultVO select() {
        String key= RedisKeys.REDIS_KEYS_DRUGS.getValue();
        //读取缓存数据
        ListOperations<String, Serializable> listOperations = redisTemplate.opsForList();
        List<Serializable> list = listOperations.range(key, 0, -1);
        assert list != null;
        if (list.size()==0){
            return new ResultVO(StatusCode.DATA_NOT_FOUND);
        }
        return ResultVO.ok(list);
    }
/*
    *//**
     * 该方法数据通过ES查询获得
     * @param //keyword 关键字
     * @return 药品列表
     *//*
    @ApiOperation("根据关键字查询药品信息")
    @GetMapping("/{keyword}/selectByRemark")
    @PreAuthorize("hasAuthority('Details')")
    public ResultVO selectByRemark(@PathVariable String keyword){
        List<DrugVo> list= searchRepository.searchByKeyword(keyword);
        return ResultVO.ok(list);
    }*/

    @ApiOperation("查询所有药品并且分页和查询")
    @PostMapping("/selectAll")
    @PreAuthorize("hasAuthority('DDD') or hasAuthority('AAA')")
    public ResultVO selectAll(@RequestBody RequestDataLimit dataLimit){
        int pageSize = dataLimit.getPageSize();
        int page = dataLimit.getPage();
        Map<String, Object> map = new HashMap<>();
        int total = mapper.getTotalDrugs(dataLimit.getDrugNo(),dataLimit.getDrugName());//总条数
        int totalPages = (int)Math.ceil((double)total/pageSize);//计算总页数
        List<DrugVo> drugs;
        if(totalPages==0){
            drugs = new ArrayList<>();
        }else {
            int offset = (page - 1) * pageSize;
            drugs = mapper.selectAll(pageSize,offset,dataLimit.getDrugNo(),dataLimit.getDrugName());
        }
        for (DrugVo drug : drugs) {
            String remark = drug.getRemark();
            if (remark.length() > 24) {
                remark = remark.substring(0, 24);
            }
            drug.setRemark(remark);
        }
        map.put("drugs", drugs);
        map.put("total", total);
        return ResultVO.ok(map);
    }
    @ApiOperation("添加药品")
    @PostMapping("/addDrug")
    public ResultVO addDrug(@RequestBody DrugDTO drugDTO){
        mapper.addDrug(drugDTO);
        return ResultVO.ok();
    }

    @ApiOperation("根据药品id删除药品")
    @DeleteMapping("/deleteDrugId/{id}")
    @PreAuthorize("hasAuthority('AAA')")
    public ResultVO deleteDrugId(@PathVariable Integer id){
        mapper.deleteDrugId(id);
        return ResultVO.ok();
    }
    @ApiOperation("根据药品id修改药品")
    @PutMapping("/updateById")
    public ResultVO updateById(@RequestBody DrugDTO drugDTO){
        mapper.updateById(drugDTO);
        return ResultVO.ok();
    }

}
